package com.cuput.partybuilding.publiclibrary.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cuput.partybuilding.publiclibrary.TempApplication;
import com.hwangjr.rxbus.SmecRxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.io.Serializable;


/**
 * Created by wei on 2017-02-22.
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements Serializable {
    public static final String ACTIVITY_FINISH = "BaseActivity.finishActivity";

    protected T mPresenter;

    private SVProgressHUD svProgressHUD ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        mPresenter =getPresenter();
        SmecRxBus.get().register(this);
        svProgressHUD = new SVProgressHUD(this);

        TempApplication.getSvProgressHUDs().add(svProgressHUD);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(BaseActivity.ACTIVITY_FINISH)
            }
    )
    public void finishActivity(String activityClass) {
        if (this.getClass().getName().equals(activityClass) || "ALL".equals(activityClass)) {
            this.finish();
        }
    }

    @Override
    protected void onDestroy() {
        TempApplication.getSvProgressHUDs().remove(svProgressHUD);
        SmecRxBus.get().unregister(this);
        super.onDestroy();
    }


    public abstract T getPresenter();
}
